package com.gdcp.service.impl;

import com.gdcp.beans.Collect;
import com.gdcp.beans.Product;
import com.gdcp.mapper.CollectMapper;
import com.gdcp.common.page.PageBean;
import com.gdcp.common.page.PageConstants;
import com.gdcp.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    CollectMapper collectMapper;

    @Override
    public int insert(Integer shop_id, String shop_name, Integer user_id) {
        Collect collect=new Collect();
        collect.setShop_id(shop_id);
        collect.setShop_name(shop_name);
        collect.setUser_id(user_id);
        try{
            collectMapper.insert(collect);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    public List<Collect> queryAllByUserId(Integer id) {
        return collectMapper.queryAllByUserId(id);
    }

    @Override
    public int delete(Integer id) {
        try{
            collectMapper.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    public PageBean<Collect> queryByPage(Integer user_id, int pc) {
        PageBean<Collect> pb = new PageBean<>();
        pb.setPc(pc);

        int ps = 8;
        pb.setPs(ps);

        //获取总数----直接一次性获取发送过来
        int tr=collectMapper.getPageNumber(user_id).size();
        pb.setTr(tr);

        pb.setTp(pb.getTp());

        //分页查询
        List<Collect> collects=collectMapper.queryByPage(user_id,(pc-1)*ps,ps);
        pb.setBeanList(collects);
        return pb;
    }

    @Override
    public int checkByShopIdAndUserId(Integer shop_id, Integer user_id) {
        return collectMapper.checkByShopIdAndUserId(shop_id, user_id).size();
    }
}
